package com.test;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.ServerSocket;
import java.util.Scanner;

public class EchoClient{

	private static ServerSocket serverSocket;
	
	public static void main(String[] args) throws Exception {
		
		serverSocket = new ServerSocket();
		serverSocket.setReuseAddress(true);
		
		Scanner localScanner = null;
		Scanner scanner = null;
		try (Socket socket = new Socket("localhost", 8000)) {
			localScanner = new Scanner(System.in);
			OutputStream outputStream = socket.getOutputStream();
			PrintWriter pWriter = new PrintWriter(outputStream, true);
			scanner = new Scanner(socket.getInputStream());
			String line = null;
			while (true) {
				line = localScanner.nextLine();
				pWriter.println(line);
				System.out.println(scanner.nextLine());
				if (line.equals("bye"))
					break;
			}
		}finally {
			if(localScanner != null)
				localScanner.close();
			if(scanner != null)
				scanner.close();
		}
	}
	
}
